package com.guodi.model.controller;



import com.guodi.common.utils.UuidUtil;
import com.guodi.core.secure.utils.AuthUtil;
import com.guodi.model.vo.ApiIdtFillValueVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.guodi.core.tool.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;


import com.guodi.common.exception.BaseRuntimeException;
import org.springframework.web.bind.annotation.RestController;
import com.guodi.core.boot.ctrl.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import com.guodi.model.service.IIdtFillValueService;
import com.guodi.model.entity.IdtFillValue;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
* 指标填报任务值表 前端控制器
* @author lzy
* @date 2023-05-09
*/
@Slf4j
@Validated
@RestController
@RequestMapping("/IdtFillValue")
@Api(value = "API - IdtFillValueController", tags = "指标填报任务值表")
public class IdtFillValueController extends BaseController {

    @Resource
    private IIdtFillValueService iIdtFillValueService;

    /**
     * @描述: 批量保存/更新指标填报值
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/5/9 17:15
     **/
    @RequestMapping("/saveOrUpdateFillValues")
    @ApiOperation(value = "批量保存/更新指标填报值")
    public R saveOrUpdateFillValues(@RequestBody List<ApiIdtFillValueVo> fillValueVos) {
        List<IdtFillValue> saveList = new ArrayList<>();
        List<IdtFillValue> updateList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(fillValueVos)){
            for(ApiIdtFillValueVo vo : fillValueVos){
                if(StringUtils.isEmpty(vo.getTaskId()) || StringUtils.isEmpty(vo.getIndicatorsId()) ){
                    return R.fail("任务id或者指标id必传");
                }
                IdtFillValue idtFillValue = new IdtFillValue();
                BeanUtils.copyProperties(vo,idtFillValue);
                if(StringUtils.isEmpty(idtFillValue.getId())){
                    prepareData(idtFillValue,true);
                    saveList.add(idtFillValue);
                }else {
                    prepareData(idtFillValue,false);
                    updateList.add(idtFillValue);
                }
            }
        }
        if(!CollectionUtils.isEmpty(saveList)){
            iIdtFillValueService.saveBatch(saveList);
        }
        if(!CollectionUtils.isEmpty(updateList)){
            iIdtFillValueService.updateBatchById(updateList);
        }
        return R.data("操作成功");
    }

    private void prepareData(IdtFillValue entity, boolean isSave) {
        if(isSave){
            entity.setId(UuidUtil.get32UUID());
            entity.setCreator(String.valueOf(AuthUtil.getUserId()));
            entity.setCreateTime(new Date());
            //防止误修改
            entity.setModifier(null);
            entity.setModifyTime(null);
        }else{
            entity.setModifier(String.valueOf(AuthUtil.getUserId()));
            entity.setModifyTime(new Date());
            //防止误修改
            entity.setCreator(null);
            entity.setCreateTime(null);
        }
    }


}
